package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.entity.PdCoreOrg;
import com.itheima.pinda.mapper.PdCoreOrgMapper;
import com.itheima.pinda.service.PdCoreOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 组织 服务实现类
 * </p>
 *
 * @author xsf
 * @since 2021-11-15
 */
@Service
public class PdCoreOrgServiceImpl extends ServiceImpl<PdCoreOrgMapper, PdCoreOrg> implements PdCoreOrgService {

    @Autowired
    private PdCoreOrgMapper pdCoreOrgMapper;


    @Override
    public List<PdCoreOrg> queryAllTree() {
        QueryWrapper<PdCoreOrg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",0);
        List<PdCoreOrg> coreOrgList = pdCoreOrgMapper.selectList(queryWrapper);
        coreOrgList.forEach(pdCoreOrg -> {
            PdCoreOrg coreOrg = queryChildren(pdCoreOrg);
            List<PdCoreOrg> childrenList = coreOrg.getChildren();
            childrenList.forEach(child -> {
                queryChildren(child);
            });
        });
        return coreOrgList;
    }

    private PdCoreOrg queryChildren(PdCoreOrg pdCoreOrg) {
        QueryWrapper<PdCoreOrg> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",pdCoreOrg.getId());
        List<PdCoreOrg> list = pdCoreOrgMapper.selectList(wrapper);
        pdCoreOrg.setChildren(list);
        return pdCoreOrg;
    }
}
